<template>
	<view class="wrap">
		<u-form :model="model" :rules="rules" ref="uForm" :errorType="errorType">
			<u-form-item :leftIconStyle="{color: '#888', fontSize: '32rpx'}" left-icon="account" label-width="150"
				:label-position="labelPosition" label="姓名" prop="driverName">
				<u-input :border="border" placeholder="请输入姓名" v-model="model.driverName" type="text"></u-input>
			</u-form-item>
			<u-form-item :label-position="labelPosition" label="车辆" prop="car" label-width="150">
				<u-input :border="border" type="select" :select-open="selectShow" v-model="model.carCode"
					placeholder="请选择提供运力的车辆" @click="selectCarCode"></u-input>
			</u-form-item>
			<u-form-item :label-position="labelPosition" label="发布内容" prop="orderTitle" label-width="150">
				<u-input type="textarea" :border="border" placeholder="如:时间、地点、运输货物期望" v-model="model.orderTitle" />
			</u-form-item>
			<u-form-item :label-position="labelPosition" label="期望回报" prop="exception" label-width="150">
				<u-input type="text" :border="border" placeholder="请输入您想要的运输回报" v-model="model.exception" />
			</u-form-item>
		</u-form>
		<u-button @click="submit">提交</u-button>
		<u-select mode="single-column" :list="carCodeList" v-model="selectShow" @confirm="selectConfirm" />
	</view>
</template>

<script>
	import {getCarCodeRequest} from '../../../../api/driver/publish.js'
	export default {
		props:{
			driverName: {
				type: String,
				default() {
					return ''
				}
			},
			driverCode: {
				type: String,
				default() {
					return ''
				}
			}
		},
		data() {
			let that = this;
			return {
				model: {
					driverName: '',
					carCode: '',
					orderTitle: '',
					exception: undefined,
					state: 0
				},
				carCodeList: [
				],
				rules: {
					driverName: [{
							required: true,
							message: '请输入姓名',
							trigger: ['change', 'blur'],
						},
						{
							min: 2,
							max: 5,
							message: '姓名长度在3到5个字符',
							trigger: ['change', 'blur'],
						},
						{
							// 此为同步验证，可以直接返回true或者false，如果是异步验证，稍微不同，见下方说明
							validator: (rule, value, callback) => {
								// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
								return this.$u.test.chinese(value);
							},
							message: '姓名必须为中文',
							// 触发器可以同时用blur和change，二者之间用英文逗号隔开
							trigger: ['change', 'blur'],
						}
						// 异步验证，用途：比如用户注册时输入完账号，后端检查账号是否已存在
						// {
						// 	trigger: ['blur'],
						// 	// 异步验证需要通过调用callback()，并且在里面抛出new Error()
						// 	// 抛出的内容为需要提示的信息，和其他方式的message属性的提示一样
						// 	asyncValidator: (rule, value, callback) => {
						// 		this.$u.post('/ebapi/public_api/index').then(res => {
						// 			// 如果验证出错，需要在callback()抛出new Error('错误提示信息')
						// 			if(res.error) {
						// 				callback(new Error('姓名重复'));
						// 			} else {
						// 				// 如果没有错误，也要执行callback()回调
						// 				callback();
						// 			}
						// 		})
						// 	},
						// }
					],
					exception: [{
							required: true,
							message: '请填写期望回报',
							trigger: ['change', 'blur'],
						},
						{
							// 此为同步验证，可以直接返回true或者false，如果是异步验证，稍微不同，见下方说明
							validator: (rule, value, callback) => {
								// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
								return this.$u.test.amount(value);
							},
							message: '金额不符合规范',
							// 触发器可以同时用blur和change，二者之间用英文逗号隔开
							trigger: ['change', 'blur'],
						},
					],
					orderTitle: [{
						required: true,
						message: '请填写运力发布的内容',
						trigger: ['change', 'blur'],
					}]
			},
			border: false,
				check: false,
				selectShow: false,
				labelPosition: 'left',
				errorType: ['message'],
		};
	},
		computed: {},
		created() {
			this.model.driverName = this.driverName;
		},
		mounted() {
			// 必须要等结点渲染完毕后才能把校验挂载上去
			this.$refs.uForm.setRules(this.rules);
		},
		computed: {
			borderCurrent() {
				return this.border ? 0 : 1;
			}
		},
		methods: {
			// 交给父组件去判断
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.$emit('submit', this.model)
						this.model = {}
					} else {
						//console.log(this.$refs.upload.beforeUpload())
						uni.showToast({
							title: '验证失败，请查看是否输入正确信息',
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			// 根据司机姓名去找他的所有车的车牌号
			selectCarCode() {
				console.log(this.driverCode);
				if(this.model.driverName) {
					getCarCodeRequest(this.driverCode)
					.then(res => {
						let temp = res.body.filter(o => o.state === 3 || o.state === 4).map(item => {
							return {
								value: item.carCode,
								label: item.carCode
							}
						})
						this.carCodeList = temp;
						this.selectShow = true;
					})
				} else {
					uni.showToast({
						title: '请输入司机姓名',
						icon: 'none',
						duration: 2000
					});
				}
				
			},
			
			// 选择车辆回调
			selectConfirm(e) {
				this.model.carCode = '';
				e.map((val, index) => {
					this.model.carCode += this.model.carCode == '' ? val.label : '-' + val.label;
				})
			}
		}
	};
</script>

<style scoped lang="scss">
	.wrap {
		padding: 30rpx;
		z-index: -10;
	}
</style>
